package com.nowcoder.code.hjnc;

import com.algorithm.model.TreeNode;

public class NC102 {


    public int lowestCommonAncestor(TreeNode root, int o1, int o2) {
        if (root == null) {
            return -1;
        }
        if (root.val == o1 || root.val == o2) return root.val;
        int l = lowestCommonAncestor(root.left, o1, o2);
        int r = lowestCommonAncestor(root.right, o1, o2);
        if (l >= 0 && r >= 0) {
            return root.val;
        } else if (l >= 0) {
            return l;
        }
        return r;
    }

}
